import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'providers/music_provider.dart';
import 'pages/main_page.dart';
import 'utils/app_theme.dart';
import 'dart:async';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => MusicProvider()..initialize(),
      child: MaterialApp(
        title: '仿·网易云音乐',
        theme: ThemeData(
          // 白色背景主题
          brightness: Brightness.light,
          primaryColor: const Color(0xFFD33A4E), // 网易云红色
          scaffoldBackgroundColor: Colors.white,
          appBarTheme: const AppBarTheme(
            backgroundColor: Colors.white,
            foregroundColor: Colors.black,
            elevation: 0,
          ),
          bottomNavigationBarTheme: const BottomNavigationBarThemeData(
            selectedItemColor: Color(0xFFD33A4E),
            unselectedItemColor: Colors.grey,
            elevation: 8,
          ),
          colorScheme: ColorScheme.fromSeed(
            seedColor: const Color(0xFFD33A4E),
            brightness: Brightness.light,
          ),
          useMaterial3: true,
        ),
        home: const MainPage(),
        debugShowCheckedModeBanner: false,
      ),
    );
  }
}

// 启动页面
class SplashScreen extends StatefulWidget {
  const SplashScreen({super.key});

  @override
  State<SplashScreen> createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen>
    with SingleTickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation<double> _fadeAnimation;

  @override
  void initState() {
    super.initState();

    // 设置淡入动画
    _animationController = AnimationController(
      duration: const Duration(seconds: 2),
      vsync: this,
    );

    _fadeAnimation = Tween<double>(
      begin: 0.0,
      end: 1.0,
    ).animate(CurvedAnimation(
      parent: _animationController,
      curve: Curves.easeInOut,
    ));

    _animationController.forward();

    // 2秒后跳转到主页面
    Timer.periodic(const Duration(seconds: 3), (timer) {
      timer.cancel();
      _navigateToMainPage();
    });
  }

  void _navigateToMainPage() {
    Navigator.of(context).pushReplacement(
      MaterialPageRoute(
        builder: (context) => const MainPage(),
      ),
    );
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFF2D2D2D), // 网易云深色背景
      body: Center(
        child: FadeTransition(
          opacity: _fadeAnimation,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              // 应用图标
              Container(
                width: 120,
                height: 120,
                decoration: BoxDecoration(
                  color: const Color(0xFFD33A4E), // 网易云红色
                  borderRadius: BorderRadius.circular(20),
                  boxShadow: [
                    BoxShadow(
                      color: const Color(0xFFD33A4E).withOpacity(0.3),
                      blurRadius: 20,
                      spreadRadius: 5,
                    ),
                  ],
                ),
                child: CustomPaint(
                  painter: NetEaseAppLogoPainter(),
                ),
              ),
              
              const SizedBox(height: 30),
              
              // 应用名称
              const Text(
                '仿·网易云音乐',
                style: TextStyle(
                  fontSize: 28,
                  fontWeight: FontWeight.bold,
                  color: Colors.white,
                ),
              ),
              
              const SizedBox(height: 10),
              
              // 副标题
              Text(
                '本地音乐播放器',
                style: TextStyle(
                  fontSize: 16,
                  color: Colors.white.withOpacity(0.7),
                ),
              ),
              
              const SizedBox(height: 50),
              
              // 加载指示器
              SizedBox(
                width: 200,
                child: LinearProgressIndicator(
                  backgroundColor: Colors.white.withOpacity(0.1),
                  valueColor: const AlwaysStoppedAnimation<Color>(
                    Color(0xFFD33A4E),
                  ),
                ),
              ),
              
              const SizedBox(height: 20),
              
              Consumer<MusicProvider>(
                builder: (context, musicProvider, child) {
                  if (musicProvider.isLoading) {
                    return const Text(
                      '正在扫描音乐库...',
                      style: TextStyle(
                        color: Colors.white70,
                        fontSize: 14,
                      ),
                    );
                  } else if (musicProvider.errorMessage.isNotEmpty) {
                    return Text(
                      musicProvider.errorMessage,
                      style: const TextStyle(
                        color: Colors.redAccent,
                        fontSize: 14,
                      ),
                    );
                  } else {
                    return Text(
                      '发现 ${musicProvider.allSongs.length} 首歌曲',
                      style: const TextStyle(
                        color: Colors.white70,
                        fontSize: 14,
                      ),
                    );
                  }
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}

// 网易云音乐应用Logo绘制器（用于启动页）
class NetEaseAppLogoPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = Colors.white
      ..style = PaintingStyle.fill;
    
    final center = Offset(size.width / 2, size.height / 2);
    final radius = size.width / 2;
    
    // 绘制外圆
    canvas.drawCircle(center, radius, paint);
    
    // 绘制内部红色音符
    final notePaint = Paint()
      ..color = const Color(0xFFD33A4E)
      ..style = PaintingStyle.fill;
    
    // 音符主体圆形
    final noteRadius = radius * 0.15;
    final noteCenter1 = Offset(center.dx - radius * 0.25, center.dy + radius * 0.1);
    final noteCenter2 = Offset(center.dx + radius * 0.25, center.dy + radius * 0.1);
    
    // 绘制两个音符圆
    canvas.drawCircle(noteCenter1, noteRadius, notePaint);
    canvas.drawCircle(noteCenter2, noteRadius * 0.8, notePaint);
    
    // 绘制音符竖线
    final linePaint = Paint()
      ..color = const Color(0xFFD33A4E)
      ..strokeWidth = radius * 0.06
      ..style = PaintingStyle.stroke
      ..strokeCap = StrokeCap.round;
    
    // 左侧音符竖线
    canvas.drawLine(
      Offset(noteCenter1.dx + noteRadius * 0.8, noteCenter1.dy - noteRadius * 0.5),
      Offset(noteCenter1.dx + noteRadius * 0.8, noteCenter1.dy - radius * 0.5),
      linePaint,
    );
    
    // 右侧音符竖线
    canvas.drawLine(
      Offset(noteCenter2.dx + noteRadius * 0.6, noteCenter2.dy - noteRadius * 0.4),
      Offset(noteCenter2.dx + noteRadius * 0.6, noteCenter2.dy - radius * 0.4),
      linePaint,
    );
    
    // 连接线（音符梁）
    final beamPath = Path();
    beamPath.moveTo(noteCenter1.dx + noteRadius * 0.8, noteCenter1.dy - radius * 0.5);
    beamPath.quadraticBezierTo(
      center.dx, center.dy - radius * 0.6,
      noteCenter2.dx + noteRadius * 0.6, noteCenter2.dy - radius * 0.4,
    );
    beamPath.lineTo(noteCenter2.dx + noteRadius * 0.6, noteCenter2.dy - radius * 0.35);
    beamPath.quadraticBezierTo(
      center.dx, center.dy - radius * 0.5,
      noteCenter1.dx + noteRadius * 0.8, noteCenter1.dy - radius * 0.45,
    );
    beamPath.close();
    
    canvas.drawPath(beamPath, notePaint);
  }
  
  @override
  bool shouldRepaint(CustomPainter oldDelegate) => false;
}
